import wx
import wx.lib.ogl as ogl

CLICK_TO_DRAG = True
class MyEvtHandler(ogl.ShapeEvtHandler):
        """
        Overwrite the default event handler to implement some custom features. 
        """
        def __init__(self):
                ogl.ShapeEvtHandler.__init__(self)

        def OnLeftClick(self, x, y, keys = 0, attachment = 0):
                """
                The dragging is done here. 
                """
                shape = self.GetShape()
                print shape.__class__, shape.GetClassName(), shape.a
                canvas = shape.GetCanvas()
                dc = wx.ClientDC(canvas)
                canvas.PrepareDC(dc)

                if shape.Selected():
                        shape.Select(False, dc)
                        canvas.Redraw(dc)
                        print "selezionato"
                else:
                        redraw = False
                        shapeList = canvas.GetDiagram().GetShapeList()
                        toUnselect = []
                        print "non selezionato"
                        for s in shapeList:
                                if s.Selected():
                                        toUnselect.append(s)

                        shape.Select(True, dc)

                        if toUnselect:
                                for s in toUnselect:
                                        s.Select(False, dc)
                                canvas.Redraw(dc)

        def OnRightClick(self, x, y, keys = 0, attachment = 0):
                """
                The dragging is done here. 
                """
                shape = self.GetShape()
                print shape.__class__, shape.GetClassName(), shape.a
                canvas = ogl.ShapeCanvas( self )
                dc = wx.ClientDC(canvas)
                self.diagram= canvas.GetDiagram()
               
                
                if shape.Selected():
                        self.diagram.RemoveShape(shape)
                        canvas.SetDiagram(self.diagram)
                        canvas.Redraw(dc)
                        
                else:
                        
                        shapeList = canvas.GetDiagram().GetShapeList()
                        
                        toUnselect = []
                        for s in shapeList:
                                if s.Selected():
                                        toUnselect.append(s)

                        shape.Select(True, dc)
                        
                        if toUnselect:
                                for s in toUnselect:
                                        s.Select(False, dc)
                                canvas.Redraw(dc)



class AppFrame(wx.Frame):
    
     
    def __init__(self):
        wx.Frame.__init__( self,
                          None, -1, "Demo",
                          size=(1200,750),
                          style=wx.DEFAULT_FRAME_STYLE )
        
        
        self.sizer = wx.GridBagSizer( 5,5 )
        # put stuff into sizer

        canvas = ogl.ShapeCanvas( self )
        self.sizer.Add( canvas, pos= (4,16),span=(16,16),flag=wx.EXPAND)
      
                            
        canvas.SetBackgroundColour( "LIGHT BLUE" ) #

        diagram = ogl.Diagram()
        canvas.SetDiagram( diagram )
        diagram.SetCanvas( canvas )
       

        self.shape = ogl.CircleShape( 20.0 )            #
        self.shape.SetX( 25.0 )                         #
        self.shape.SetY( 25.0 )  
        self.shape.a="Circle identified"
                        #
        canvas.AddShape( self.shape )                   #
        
        
        diagram.ShowAll( 1 )            #

        if CLICK_TO_DRAG:
            self.evthandler = MyEvtHandler()
            self.evthandler.SetShape(self.shape)
            self.evthandler.SetPreviousHandler(self.shape.GetEventHandler())
            self.shape.SetEventHandler(self.evthandler)

         #apply sizer
        self.SetSizer(self.sizer)
        self.SetAutoLayout(1)
        self.Show(1)
        self.Center()
        




app = wx.PySimpleApp()
ogl.OGLInitialize()
frame = AppFrame()

app.MainLoop()

app.Destroy()